package com.zsc.newbuds.mapper;

import com.zsc.newbuds.entity.Shop;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ShopMapper {
    /**
     * 获取所有商品信息
     * @return
     */
    @Select("select * from shop")
    List<Shop> getGoodsList();

    /**
     * 增加商品
     * @param shop
     */
    @Options(useGeneratedKeys = true,keyProperty = "goodsId")
    @Insert("insert into shop(goodsName,goodsPrice,goodsImage,goodsOperator,goodsCount) values(#{goodsName},#{goodsPrice},#{goodsImage},#{goodsOperator},#{goodsCount})")
    void addGoods(Shop shop);

    /**
     * 查询商品根据id
     * @param id
     * @return
     */
    @Select("select * from shop where goodsId = #{id}")
    Shop getGoodsById(Integer id);


    /**
     * 获取商品主要信息用于前台展示
     * @param page
     * @param limit
     * @param limit
     * @return
     */
    @Select("select goodsId,goodsName,goodsPrice,goodsCount,goodsImage from shop where goodsCount>0 limit #{page},#{limit}")
    List<Shop> getGoodsMainList(Integer page,Integer limit);

    /**
     * 兑换成功商品数量更新
     * @param goodsId
     * @param count
     * @param version
     */
    @Update("update shop set goodsCount = #{count},version = #{version} where goodsId = #{goodsId}")
    void updateShopCount(Integer goodsId,int count,int version);

    /**
     * 后台增加商品数量
     * @param goodsId
     * @param count
     */
    @Update("update shop set goodsCount = #{count} where goodsId = #{goodsId}")
    void updateCount(Integer goodsId,int count);
}
